package com.sunlands.zlcx.usercenter.service;

import com.sunlands.zlcx.usercenter.domain.log.LoginLogDO;
import com.sunlands.zlcx.usercenter.domain.log.UserActiveLogDO;
import com.sunlands.zlcx.usercenter.domain.log.UserOnlineLogDO;
import com.sunlands.zlcx.usercenter.repository.LoginLogRepository;
import com.sunlands.zlcx.usercenter.repository.UserActiveLogRepository;
import com.sunlands.zlcx.usercenter.repository.UserOnlineLogRepository;
import com.sunlands.zlcx.usercenter.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.exception.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.sunlands.zlcx.usercenter.config.RedisConfig.CacheKeys.USERS_ACTIVE;

/**
 * @author anjl
 * @date 6:23 PM 2019/7/11
 * description:
 */
@Slf4j
@Service
public class LogServiceImpl {

    /**
     * 保存user login日志 (异步处理)
     *
     * @param loginLogDO loginLogDO
     * @return LoginLogDO
     */
    @Async
    public LoginLogDO save(LoginLogDO loginLogDO) {
        try {
            return loginLogRepository.save(loginLogDO);
        } catch (Exception e) {
            log.error("save loginLogDO = " + loginLogDO, e);
        }
        return null;
    }

    /**
     * 保存用户活跃日志（异步处理）一天只保存一条
     *
     * @param userActiveLogDO userActiveLogDO
     * @return UserActiveLogDO
     */
    @Async
    @Transactional
    public UserActiveLogDO save(UserActiveLogDO userActiveLogDO) {
        Boolean exists = userActiveLogRepository.existsByUserIdAndDate(userActiveLogDO.getUserId(), DateUtil.getStringDateShort());
        if (exists) {
            return null;
        }
        try {
            return userActiveLogRepository.save(userActiveLogDO);
        } catch (ConstraintViolationException | DataIntegrityViolationException e) {
            //唯一索引报错，重复插入导致，不发邮件了
            e.printStackTrace();
        }
        return null;
    }

    @Cacheable(cacheNames = USERS_ACTIVE, key = "#date+'-'+#userId")
    public Boolean findByUserId(String date, Long userId) {
        return userActiveLogRepository.existsByUserIdAndDate(userId, date) ? true : null;
    }

//    public List<UserActiveLogDO> findByCreateTimeBetween(Date start, Date end){
//        return userActiveLogRepository.findByCreateTimeBetween(start, end);
//    }

    public UserOnlineLogDO getOnlineLog(Long channelId, Long userId) {
        return userOnlineLogRepository.findByChannelIdAndUserId(channelId, userId);
    }

    public UserOnlineLogDO saveOnlineLog(UserOnlineLogDO onlineLog) {
        return userOnlineLogRepository.save(onlineLog);
    }

    @Autowired
    private LoginLogRepository loginLogRepository;
    @Autowired
    private UserActiveLogRepository userActiveLogRepository;
    @Autowired
    private UserOnlineLogRepository userOnlineLogRepository;
}
